package st.business.tepth.business.visitor.visitorwork.work;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import me.yokeyword.fragmentation.SupportFragment;
import st.business.tepth.R;
import st.business.tepth.R2;
import st.business.tepth.base.OnMultiItemChooseListener;
import st.business.tepth.business.user.busnumber.workholder.adapter.ClassificationAdapter;
import st.business.tepth.business.visitor.visitorwork.work.detail.DetailDelegate;
import st.shadow.library.core.base.mvp.MvpFragment;
import st.shadow.library.ui.recycler.MultipleItemEntity;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-06-05 16:31
 */

public class WorkDelegate extends MvpFragment<WorkPresenter> implements WorkContract.View {
    public static WorkDelegate newInstance(int repairId, String title) {
        Bundle bundle = new Bundle();
        bundle.putInt("repairId", repairId);
        bundle.putString("title", title);
        WorkDelegate delegate = new WorkDelegate();
        delegate.setArguments(bundle);
        return delegate;
    }

    @BindView(R2.id.recycler_view_tab)
    RecyclerView mRecyclerView;
    @BindView(R2.id.layout_container)
    ConstraintLayout mLayoutContainer;
    @BindView(R2.id.tv_tile)
    TextView mTvTitle;
    private int mRepairId;
    private String mTitle;
    private ClassificationAdapter mClassificationAdapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mRepairId = bundle.getInt("repairId");
            mTitle = bundle.getString("title");
        }
    }

    @Override
    public WorkPresenter getPresenter() {
        return new WorkPresenter(this);
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_work_read_only;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mTvTitle.setText(mTitle);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(_mActivity));
        RecyclerView.ItemAnimator itemAnimator = mRecyclerView.getItemAnimator();
        if (itemAnimator != null) {
            itemAnimator.setChangeDuration(0);
        }
        mRecyclerView.setAdapter(
                mClassificationAdapter = new ClassificationAdapter(R.layout.item_user_classification,
                        null, new OnMultiItemChooseListener<Integer, Integer>() {
                    @Override
                    public void onChoose(Integer position,Integer id) {
                        //小修
                        SupportFragment contentFragment = findChildFragment(DetailDelegate.class);
                        if (contentFragment != null) {
                            contentFragment.replaceFragment(DetailDelegate
                                    .newInstance(id), true);
                        }
                    }
                }, false));
        addTopView(R.layout.view_loading, mLayoutContainer);
        mPresenter.getClassificationList(mRepairId);
    }

    @Override
    public void showClassification(List<MultipleItemEntity> list) {
        removeTopView(mLayoutContainer);
        if (list != null && !list.isEmpty()) {
            mClassificationAdapter.setNewData(list);
            if (findChildFragment(DetailDelegate.class) == null) {
                loadRootFragment(R.id.view_content,
                        DetailDelegate
                                .newInstance((Integer) list.get(0).getField("id")),
                        false, false);
            }
        } else {
            addTopView(R.layout.view_search_empty, mLayoutContainer);
        }
    }

    @OnClick({R2.id.tv_back})
    public void onBack() {
        _mActivity.onBackPressed();
    }
}